<template>
	<view class="content">
		<view class="login_content app_content_color">
			<view class="input_content app_background_color">
				<input class="input_text app_title_color" type="text" placeholder="请输入用户名" @input="usernameInput" />
			</view>
			<view class="input_content app_background_color">
				<input class="input_text app_title_color" placeholder="请输入密码" @input="passwordInput" />
			</view>
			<view class="login_button app_background_color app_title_color" @tap="loginTap">
				<view>登录</view>
			</view>
		</view>
	</view>
</template>


<style lang="scss">
	.content {
		display: flex;
		width: 100%;
		flex-direction: column;
	}

	.login_content {
		display: flex;
		flex-direction: column;
		margin-left: 30upx;
		margin-right: 30upx;
		margin-top: 100upx;
		border-radius: 8upx;
	}

	.input_content {
		margin-left: 30upx;
		margin-right: 30upx;
		border-radius: 8upx;
		margin-top: 30upx;
		height: 100upx;
	}

	.input_text {
		height: 100%;
		font-size: 30upx;
		flex-grow: 1;
		margin-left: 20upx;
		margin-right: 20upx;
	}

	.login_button {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin-top: 30upx;
		margin-left: 30upx;
		margin-right: 30upx;
		margin-bottom: 30upx;
		height: 100upx;
		border-radius: 50upx;
	}
</style>

<script>
	import http from '../../utils/http.js'

	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		onLoad(options) {

		},
		methods: {
			usernameInput(e) {
				this.username = e.detail.value;
			},
			passwordInput(e) {
				this.password = e.detail.value;
			},
			loginTap() {
				if (this.username.length == 0) {
					uni.showToast({
						title: "尚未输入用户名",
						icon: "none"
					})
					return;
				}
				if (this.password.length == 0) {
					uni.showToast({
						title: "尚未输入密码",
						icon: "none"
					})
					return;
				}
				
				let that = this
				http.request("user/login", {
					username: this.username,
					password: this.password
				}, {
					success: function(data) {
						uni.setStorage({
							key: 'uni_user_data',
							data: data,
							success: function() {
								console.log(JSON.stringify(data))
								http.setAccessSecret(data.secret);
								http.setAccessToken(data.token);
								getApp().globalData.user = data
								uni.navigateBack({
									
								})
							}
						});

					},
					fail: function(msg) {
						uni.showToast({
							title: msg,
							icon: "none"
						})
					}
				})


			}
		}
	}
</script>
